<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
  </style>
</head>

<body>
  <script type="module">
    import * as THREE from "../node_modules/three/build/three.module.js"
    // import { OrbitControls } from "../node_modules/three/addons/controls"
    // 创建场景
    const scane = new THREE.Scene()

    // scane.background = new THREE.Color(0xc89e06b)
    const loader = new THREE.TextureLoader();
    // 异步
    // loader.load("../image/Snipaste_2024-03-22_17-00-18.png", function (texture) {
    //   scane.background = texture;
    //   // renderer.render(scane, camera)
    // })

    // 创建相机
    const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)
    camera.position.set(2, 200, 20)
    // camera.position.x = 2;
    camera.lookAt(0, 0, 0);
    // 创建渲染器
    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    const cube = new THREE.Mesh(
      new THREE.BoxGeometry(10, 10, 10),
      new THREE.MeshBasicMaterial()
    )

    scane.add(cube);

    // 轨道控制器
    // const controls = new OrbitControls(camera, renderer.domElement)

    const gridHeliper = new THREE.GridHelper(100, 10, 0xff11ff)
    scane.add(gridHeliper, cube)

    function animate() {
      requestAnimationFrame(animate)
      renderer.render(scane, camera)
      // camera.lookAt(0, 0, 0);
    }
    animate()
    // renderer.render(scane, camera)

    let isDown = false
    let startx = 0
    let currentX = 0
    renderer.domElement.addEventListener("mousedown", (e) => {
      isDown = true
      startx = e.clientX;
    })
    renderer.domElement.addEventListener("mousemove", (e) => {
      if (isDown) {
        const distanceX = e.clientX - startx
        currentX += distanceX * 0.009
        // console.log(currentX);
 
        startx = e.clientX
        camera.position.z = 40 * Math.sin(currentX)
        camera.position.x = 40 * Math.cos(currentX)
        camera.lookAt(0, 0, 0)
      }
    })
    renderer.domElement.addEventListener("mouseup", (e) => {
      isDown = false
    })
  </script>
</body>

</html>